package henu.dao.impl;

import henu.bean.Permission;
import henu.bean.Role;
import henu.dao.PermissionDao;
import henu.dao.factory.DaoFactory;
import henu.util.Dbcp;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import oracle.net.aso.i;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;


/**
 * 权限管理
 * 15-03-20
 * @author wuhaifeng 
 * */
public class PermissionDaoImpl implements PermissionDao{


	public static void main(String[] args) {
		PermissionDaoImpl impl = new PermissionDaoImpl();
		List<Permission> list = impl.findByProperty("pname", "xitong", "pid", "asc");
		System.out.println(list.toString());
		
	}
	private List<Permission> listPermission ; 
	private QueryRunner runner ; 

	/**
	 * 根据权限的id找到这个权限
	 * 
	 * @param pid
	 *            权限的id
	 * */
	public Permission findPermisstionById(int pid) {
		Permission p = null;
		QueryRunner runner = DaoFactory.getRunner();
		String sql = "SELECT * from Permission where  pid  = ?" ;
		try {
			p = runner.query(sql, new BeanHandler<Permission>(Permission.class),pid);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return p;
	}


	@Override
	public List<Permission> findAllPermission()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Permission> findByProperty(String property, String key,
			String order, String sort)
	{
		listPermission = new ArrayList<Permission>();
		runner = DaoFactory.getRunner();
		String sql = "SELECT * from permission where  " + property + "= ?"
				+ " order by " + order + " " + sort;
		try {
			listPermission = runner.query(sql, new BeanListHandler<Permission>(
					Permission.class), key);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return listPermission;
	}


	



}